<?php

namespace app\common\model\Games;

use think\Model;

class Gameuser extends Model
{
    protected $name = 'gameuser'; //表名

    protected $autoWriteTimestamp = true;

    protected $createTime = 'createtime'; //时间戳的字段名

    protected $updateTime = false; //关闭更新时自动写入

    protected $field = true; //过滤

    //隐藏
    protected $hidden = [
        'password',
        'salt',
    ];

    protected $append = [
        "vipendtime_text",
        // "usernumber_text",
        "avatar_text"
    ];

    public function getVipendtimeTextAttr($value, $data)
    {
        $vipendtime = isset($data['vipendtime']) ? $data['vipendtime'] : '';
        if ($vipendtime) {
            return date('Y-m-d H:i', $vipendtime);
        } else {
            return '无';
        }
    }

    public function getAvatarTextAttr($value, $data)
    {
        $avatar = $data['avatar'] ?: '';

        if (empty($avatar) || !is_file('.' . $avatar)) {
            $avatar = '/assets/img/avatar.png';
        }

        // 返回一个带有域名的下载地址
        return request()->domain() . $avatar;
    }
}
